var app = angular.module('ieFunds', ['ngFileUpload', 'ui.router', 'ui.bootstrap', 'ngAnimate', 'ui.tree', 'mePagination']);

app.run(function ($rootScope, $state, $stateParams) {
    $rootScope.state = $state;
    $rootScope.stateParams = $stateParams;
    $rootScope.param = {
        fieldName: "",
        value: ""
    };

    $rootScope.myPage={
        currentPage:1,      //访问第几页数据，从1开始
        totalItems:0,       //数据库中总共有多少条数据
        itemsPerPage: 5,    //默认每页展示多少条数据，可更改
        pagesLength: 5,
        perPageOptions: [5, 10, 20, 30, 40, 50]    //可选择的每页展示多少条数据
    };
});

// HttpInterceptor
app.factory('HttpInterceptor', function ($q, $injector) {
    return {
        request: function (config) {
            return config;
        },
        requestError: function (err) {
            return $q.reject(err);
        },
        response: function (res) {
            return res;
        },
        responseError: function (err) {
            var stateService = $injector.get('$state');
            if (-1 === err.status) {
            } else if (500 === err.status) {
                stateService.go('index.500');
            } else if (404 === err.status) {
                stateService.go('index.404');
            }
            return $q.reject(err);
        }
    };
})

// AngularJS trust html
    .filter('trustHtml', function ($sce) {
        return function (input) {
            return $sce.trustAsHtml(input);
        }
    })

// Route
app.config(function ($stateProvider, $urlRouterProvider, $locationProvider,
                     $httpProvider) {
    $httpProvider.interceptors.push('HttpInterceptor');
    $urlRouterProvider.otherwise('/index');
    $stateProvider
    //index
        .state('index', {
            url: '/index',
            views: {
                'home': {
                    templateUrl: 'html/main/home.html',
                },
                'navbar-side@index': {
                    templateUrl: 'html/main/navbar-side.html',
                },
                'navbar-header@index': {
                    templateUrl: 'html/main/navbar-header.html',
                },
                'navbar-footer@index': {
                    templateUrl: 'html/main/navbar-footer.html',
                },
                'content@index': {
                    templateUrl: 'html/main/content.html',
                },
            }
        })
        //404
        .state('index.404', {
            url: '/404',
            views: {
                'content@index': {
                    templateUrl: 'html/exception/404.html',
                }
            }
        })
        //500
        .state('index.500', {
            url: '/500',
            views: {
                'content@index': {
                    templateUrl: 'html/exception/500.html',
                }
            }
        })
        //stock
        .state('index.stock', {
            url: '/stock',
            views: {
                'content@index': {
                    templateUrl: 'html/stock/index.html',
                    controller: StockIndexController
                }
            },
            params: {
                stockName: ''
            }
        })
        .state('index.stock.detail', {
            url: '/detail',
            views: {
                'content@index': {
                    templateUrl: 'html/stock/detail.html',
                    controller: StockDetailController
                }
            }
        })

        //fund
        .state('index.fund', {
            url: '/fund',
            views: {
                'content@index': {
                    templateUrl: 'html/fund/index.html',
                    controller: FundIndexController
                }
            }
        })
        .state('index.fund.add', {
            url: '/add',
            views: {
                'content@index': {
                    templateUrl: 'html/fund/add.html',
                    controller: FundAddController
                }
            }
        })
        .state('index.fund.edit', {
            url: '/edit',
            views: {
                'content@index': {
                    templateUrl: 'html/fund/edit.html',
                    controller: FundEditController
                }
            },
            params: {
                fundUid: ''
            }
        })

        //transaction
        .state('index.transaction', {
            url: '/transaction',
            views: {
                'content@index': {
                    templateUrl: 'html/transaction/index.html',
                    controller: TransactionIndexController
                }
            }
        })

        //user
        .state('index.user', {
            url: '/user',
            views: {
                'content@index': {
                    templateUrl: 'html/user/index.html',
                    controller: UserIndexController
                }
            }
        })
});


